package cn.stylefeng.guns.modular.transport.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.basic.model.params.BasicCarrierParam;
import cn.stylefeng.guns.modular.basic.model.result.BasicCarrierResult;
import cn.stylefeng.guns.modular.basic.service.BasicCarrierService;
import cn.stylefeng.guns.modular.basic.service.BasicOperateService;
import cn.stylefeng.guns.modular.transport.entity.TransportCarrierFreight;
import cn.stylefeng.guns.modular.transport.model.params.TransportCarrierFreightParam;
import cn.stylefeng.guns.modular.transport.service.TransportCarrierFreightService;
import cn.stylefeng.guns.sys.modular.system.entity.Role;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.service.RoleService;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


/**
 * 承运商运费信息表控制器
 *
 * @author like
 * @Date 2024-10-15 15:31:43
 */
@Controller
@RequestMapping("/transportCarrierFreight")
public class TransportCarrierFreightController extends BaseController {

    private String PREFIX = "/transport/transportCarrierFreight";

    @Autowired
    private TransportCarrierFreightService transportCarrierFreightService;

    @Autowired
    private UserService userService;

    @Autowired
    private RoleService roleService;

    @Autowired
    private BasicCarrierService basicCarrierService;

    @Autowired
    private BasicOperateService basicOperateService;

    /**
     * 跳转到主页面
     *
     * @author like
     * @Date 2024-10-15
     */
    @RequestMapping("")
    public String index(Model model) {
        User user = userService.getcurrentUser();
        Long deptId = user.getDeptId();
        Long deliverId = user.getDeliverId();
        model.addAttribute("basicOperatelist", basicOperateService.findAvailableList(deptId, deliverId, user.getOperateId()));
        // 承运商
        BasicCarrierParam carrierParam = new BasicCarrierParam();
        carrierParam.setDeliverId(deliverId);
        carrierParam.setDeptId(deptId);
        carrierParam.setOperateId(user.getOperateId());
        List<BasicCarrierResult> carrierLists = basicCarrierService.findListBySpec(carrierParam, user.getOperateId());
        model.addAttribute("carrierLists", carrierLists);
        return PREFIX + "/transportCarrierFreight.html";
    }

    /**
     * 新增页面
     *
     * @author like
     * @Date 2024-10-15
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/transportCarrierFreight_add.html";
    }

    /**
     * 编辑页面
     *
     * @author like
     * @Date 2024-10-15
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/transportCarrierFreight_edit.html";
    }

    /**
     * 新增接口
     *
     * @author like
     * @Date 2024-10-15
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(TransportCarrierFreightParam transportCarrierFreightParam) {
        this.transportCarrierFreightService.add(transportCarrierFreightParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author like
     * @Date 2024-10-15
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(TransportCarrierFreightParam transportCarrierFreightParam) {
        this.transportCarrierFreightService.update(transportCarrierFreightParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author like
     * @Date 2024-10-15
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(TransportCarrierFreightParam transportCarrierFreightParam) {
        this.transportCarrierFreightService.delete(transportCarrierFreightParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author like
     * @Date 2024-10-15
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(TransportCarrierFreightParam transportCarrierFreightParam) {
        TransportCarrierFreight detail = this.transportCarrierFreightService.getById(transportCarrierFreightParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询财务已确定
     *
     * @author like
     * @Date 2024-10-15
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(TransportCarrierFreightParam transportCarrierFreightParam,String condition ) {
        User user = userService.getcurrentUser();
        String roleId = user.getRoleId();
        if(ToolUtil.isNotEmpty(roleId)){
            for(String rId : roleId.split(",")){
                Role byId = roleService.getById(rId);
                if(byId != null && byId.getName().contains("承运商")){
                    BasicCarrierParam param = new BasicCarrierParam();
                    param.setUserId(user.getUserId());
                    BasicCarrierResult bySpec = basicCarrierService.findBySpec(param, user.getOperateId());
                    if(bySpec != null){
                        transportCarrierFreightParam.setCarrierId(bySpec.getId());
                    }else{
                        transportCarrierFreightParam.setCarrierName(user.getName());
                    }
                }
            }
        }
        transportCarrierFreightParam.setDeptId(user.getDeptId());
        transportCarrierFreightParam.setDeliverId(user.getDeliverId());
        Long operateId = user.getOperateId();
        if (operateId != null){
            transportCarrierFreightParam.setOperateId(Math.toIntExact(operateId));
        }
        LayuiPageInfo pageBySpec = this.transportCarrierFreightService.findPageBySpec(transportCarrierFreightParam, condition);
        return pageBySpec;
    }

    /**
     * 查询列表承运商已确定
     *
     * @author like
     * @Date 2024-10-15
     */
    @ResponseBody
    @RequestMapping("/list2")
    public LayuiPageInfo list2(TransportCarrierFreightParam transportCarrierFreightParam,String condition ) {
        User user = userService.getcurrentUser();
        String roleId = user.getRoleId();
        if(ToolUtil.isNotEmpty(roleId)){
            for(String rId : roleId.split(",")){
                Role byId = roleService.getById(rId);
                if(byId != null && byId.getName().contains("承运商")){
                    BasicCarrierParam param = new BasicCarrierParam();
                    param.setUserId(user.getUserId());
                    BasicCarrierResult bySpec = basicCarrierService.findBySpec(param, user.getOperateId());
                    if(bySpec != null){
                        transportCarrierFreightParam.setCarrierId(bySpec.getId());
                    }else{
                        transportCarrierFreightParam.setCarrierName(user.getName());
                    }
                }
            }
        }
        transportCarrierFreightParam.setDeptId(user.getDeptId());
        transportCarrierFreightParam.setDeliverId(user.getDeliverId());
        Long operateId = user.getOperateId();
        if (operateId != null){
            transportCarrierFreightParam.setOperateId(Math.toIntExact(operateId));
        }
        return this.transportCarrierFreightService.findPageBySpec2(transportCarrierFreightParam,condition);
    }

    /**
     * 查询列表财务最终确定
     *
     * @author like
     * @Date 2024-10-15
     */
    @ResponseBody
    @RequestMapping("/list3")
    public LayuiPageInfo list3(TransportCarrierFreightParam transportCarrierFreightParam,String condition ) {
        User user = userService.getcurrentUser();
        String roleId = user.getRoleId();
        if(ToolUtil.isNotEmpty(roleId)){
            for(String rId : roleId.split(",")){
                Role byId = roleService.getById(rId);
                if(byId != null && byId.getName().contains("承运商")){
                    BasicCarrierParam param = new BasicCarrierParam();
                    param.setUserId(user.getUserId());
                    BasicCarrierResult bySpec = basicCarrierService.findBySpec(param, user.getOperateId());
                    if(bySpec != null){
                        transportCarrierFreightParam.setCarrierId(bySpec.getId());
                    }else{
                        transportCarrierFreightParam.setCarrierName(user.getName());
                    }
                }
            }
        }
        transportCarrierFreightParam.setDeptId(user.getDeptId());
        transportCarrierFreightParam.setDeliverId(user.getDeliverId());
        Long operateId = user.getOperateId();
        if (operateId != null){
            transportCarrierFreightParam.setOperateId(Math.toIntExact(operateId));
        }
        return this.transportCarrierFreightService.findPageBySpec3(transportCarrierFreightParam,condition);
    }

    /**
     * 查询列表承运商申诉
     *
     * @author like
     * @Date 2024-10-15
     */
    @ResponseBody
    @RequestMapping("/list4")
    public LayuiPageInfo list4(TransportCarrierFreightParam transportCarrierFreightParam,String condition ) {
        User user = userService.getcurrentUser();
        String roleId = user.getRoleId();
        if(ToolUtil.isNotEmpty(roleId)){
            for(String rId : roleId.split(",")){
                Role byId = roleService.getById(rId);
                if(byId != null && byId.getName().contains("承运商")){
                    BasicCarrierParam param = new BasicCarrierParam();
                    param.setUserId(user.getUserId());
                    BasicCarrierResult bySpec = basicCarrierService.findBySpec(param, user.getOperateId());
                    if(bySpec != null){
                        transportCarrierFreightParam.setCarrierId(bySpec.getId());
                    }else{
                        transportCarrierFreightParam.setCarrierName(user.getName());
                    }
                }
            }
        }
        transportCarrierFreightParam.setDeptId(user.getDeptId());
        transportCarrierFreightParam.setDeliverId(user.getDeliverId());
        Long operateId = user.getOperateId();
        if (operateId != null){
            transportCarrierFreightParam.setOperateId(Math.toIntExact(operateId));
        }
        return this.transportCarrierFreightService.findPageBySpec4(transportCarrierFreightParam,condition);
    }

}


